<?php
include_once '../includes/common.php';
if($islogin2==1){}else sysmsg("您还未登录，请先登录",2,'./login.php',true);
if($userrow['power']==0){
    $text='购买';
    $texts='购 买';
}else{
    $text='添加';
    $texts='添 加';
}
$title=$text.'授权';
include_once './header.php';
$program = $DB->get_row("select * from authguao_program where id='" . $userrow['proid'] . "' limit 1");
if($program){
    if($userrow['power']==2){
        $money=$program['sqprice3'];
    }elseif($userrow['power']==1){
        $money=$program['sqprice2'];
    }else{
        $money=$program['sqprice'];
    }
}else{
    $money='归属程序错误';
}
?>
<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-header">
                <?php echo $text?>授权
            </div>
            <div class="card-body">
            <?php if($userrow['power']<3){?>
                <div class="layui-elem-quote">我的余额：<?php echo $userrow['rmb'];?><?php if($userrow['power']>0){?></br>授权成本：<?php echo $money;?></br>归属程序：<?php echo $program['name'];?><?php }?></div>
            <?php }?>
            <?php if($userrow['power']==0 || $userrow['power']==3){?>
                <form class="form-horizontal layui-form">
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">归属程序：</label>
                        <select name="proid" class="form-control" lay-search lay-filter="proid">
                        <?php
                        if ($conf['active_program'] == 1) {
                            $rs=$DB->query("SELECT * FROM authguao_program WHERE active=1 order by id desc");
                        } else {
                            $rs=$DB->query("SELECT * FROM authguao_program WHERE 1 order by id desc");
                        }
                        while($res = $DB->fetch($rs))
                        {
                        if($userrow['power']==3){
                            $price='(无需成本)';
                        }elseif($userrow['power']==0){
                             $price='(成本：'.$res['sqprice'].')';
                        }else{
                             $price='(成本：'.$res['sqprice'].')';
                        }
                            echo '<option value="'.$res['id'].'">'.$res['name'].$price.'</option>';
                        }
                        ?>
                        </select>
                    </div>
                <?php }?>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">站点名称：</label>
                        <input type="text" name="name" class="form-control" placeholder="购买授权的网站名称" autocomplete="off" lay-verType="tips" lay-verify="required"/>
                    </div>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">授权Q Q：</label>
                        <input type="number" name="qq" class="form-control" placeholder="购买授权的QQ" autocomplete="off" lay-verType="tips" lay-verify="required"/>
                    </div>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">授权域名：</label>
                        <input type="text" name="url" class="form-control" placeholder="支持泛域名授权,例如：*.qq.com" autocomplete="off" lay-verType="tips" lay-verify="required"/>
                    </div>
                    <button type="submit" class="btn btn-block btn-xs btn-outline-success" lay-submit lay-filter="submit_addsite"><?php echo $texts?></button>
                </form>
            </div>
        </div>
    </div>
</div>
<?php
include_once './bottom.php';
?>
<script type="text/javascript">
layui.use(['form'], function () {
    var form = layui.form;
    form.on('submit(submit_addsite)', function (data) {
        layer.alert('是否要执行当前操作？', {
            icon: 3, btn: ['确定', '取消'], btn1: function (layero, index) {
                addsite();
            }
        });
        return false;
    });
});
function addsite() {
    var proid = $("select[name='proid']").val();
    var name = $("input[name='name']").val();
    var qq = $("input[name='qq']").val();
    var url = $("input[name='url']").val();
    if (qq.length < 5 || qq.length > 10 || isNaN(qq)) {
        layer.msg('请输入5~10位QQ账号', {icon: 5});
        return false;
    } else if (url == "") {
        layer.msg("请输入正确域名", {icon: 5});
        return false;
    }
    var ii = layer.msg('正在<?php echo $text?>授权中,请稍后...', {icon: 16, time: 10 * 1000});
    $.ajax({
        type: "POST",
        url: "ajax.php?act=addsite",
        data : {proid:proid,name:name,qq:qq,url:url},
        dataType: "json",
        success: function(data) {
            layer.close(ii);
            if (data.code == 0) {
                layer.msg(data.msg, {
                    icon: 6,
                    end: function (layero, index) {
                        window.location.href = './authlist.php';
                    }
                });
            } else {
                layer.msg(data.msg, {icon: 5});
            }
        },
        error:function(data){
            layer.msg('服务器错误', {icon: 5});
            return false;
        }
    });
    return false;
};
</script>